package com.hmkj.crm.billing.service.relevance.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmkj.crm.billing.domain.order.Order;
import com.hmkj.crm.billing.domain.order.PurchaseOrder;
import com.hmkj.crm.billing.domain.relevance.RelProductInventory;
import com.hmkj.crm.billing.dto.ProductInventoryDTO;
import com.hmkj.crm.billing.mapper.relevance.RelProductInventoryMapper;
import com.hmkj.crm.billing.service.company.DeliveryCompaniesInfoService;
import com.hmkj.crm.billing.service.product.ProductInfoService;
import com.hmkj.crm.billing.service.relevance.ProductInventoryDetailService;
import com.hmkj.crm.billing.service.relevance.RelProductInventoryService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Rel产品库存服务Impl
 *
 * @author sks.lwei
 * @date 2020/01/22
 */
@Log4j2
@Service
public class RelProductInventoryServiceImpl extends ServiceImpl<RelProductInventoryMapper, RelProductInventory> implements RelProductInventoryService {

    @Resource
    private DeliveryCompaniesInfoService deliveryCompaniesInfoService;

    @Resource
    private ProductInfoService productInfoService;

    /**
     * 产品库存关联表明细
     */
    @Resource
    private ProductInventoryDetailService productInventoryDetailService;

    @Override
    public IPage<ProductInventoryDTO> listProductInventoryPage(Page<RelProductInventory> page, Wrapper<RelProductInventory> wrapper) {
        Page<RelProductInventory> relProductInventoryPage = this.page(page, wrapper);
        return relProductInventoryPage.convert(rel -> {
            ProductInventoryDTO inventoryDTO = new ProductInventoryDTO();
            BeanUtil.copyProperties(rel, inventoryDTO);
            //装载配送公司&产品 名称
            String deliveryCompaniesName = deliveryCompaniesInfoService.getDeliveryCompaniesNameByUuid(inventoryDTO.getDeliveryUuid());
            String productName = productInfoService.getProductNameByUuid(inventoryDTO.getProductUuid());
            inventoryDTO.setDeliveryName(deliveryCompaniesName);
            inventoryDTO.setProductName(productName);

            return inventoryDTO;
        });
    }

    /**
     * 增加
     *
     * @param purchaseOrder 采购订单
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void up(PurchaseOrder purchaseOrder) {
        //产品Uuid
        String productUuid = purchaseOrder.getProductUuid();
        //交付Uuid
        String deliveryUuid = purchaseOrder.getDeliveryUuid();
        //数量
        Integer number = purchaseOrder.getNumber();

        log.info("产品[{}], 增加库存[{}]件", productUuid, number);

        QueryWrapper<RelProductInventory> wrapper = new QueryWrapper<>();
        wrapper.eq("product_uuid", productUuid);
        wrapper.eq("delivery_uuid", deliveryUuid);
        RelProductInventory relProductInventory = this.getOne(wrapper);

        if(relProductInventory == null){
            log.info("[{}]新增库存[{}]件", productUuid, number);
            relProductInventory = new RelProductInventory(productUuid, deliveryUuid, number);
            relProductInventory.setUuid("relPI_"+ IdWorker.get32UUID());
            this.save(relProductInventory);
        }else {
            Integer storeNumber = relProductInventory.getNumber();
            storeNumber += number;
            UpdateWrapper<RelProductInventory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("number", storeNumber);
            updateWrapper.eq("id", relProductInventory.getId());
            log.info("[{}]增加库存[{}]->[{}]", productUuid, relProductInventory.getNumber(), storeNumber);
            this.update(updateWrapper);
        }

        productInventoryDetailService.addDetail(relProductInventory.getUuid(), purchaseOrder);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void down(String deliveryUuid, Order order) {
        //产品Uuid
        String productUuid = order.getProductUuid();
        //数量
        Integer number = order.getNumber();
        log.info("产品[{}], 删减库存[{}]件", productUuid, number);

        QueryWrapper<RelProductInventory> wrapper = new QueryWrapper<>();
        wrapper.eq("product_uuid", productUuid);
        wrapper.eq("delivery_uuid", deliveryUuid);
        RelProductInventory relProductInventory = this.getOne(wrapper);

        if(relProductInventory == null){
            throw new NullPointerException("产品库存为空！无法减少！");
        }

        Integer storeNumber = relProductInventory.getNumber();
        storeNumber = storeNumber - number;

        if(storeNumber < 0){
            throw new IllegalArgumentException("库存不足！"+storeNumber);
        }

        UpdateWrapper<RelProductInventory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("number", storeNumber);
        updateWrapper.eq("id", relProductInventory.getId());
        log.info("[{}]减少库存[{}]->[{}]", productUuid, relProductInventory.getNumber(), storeNumber);
        this.update(updateWrapper);

        productInventoryDetailService.addDetail(relProductInventory.getUuid(), order);
    }
}
